package com.atguigu.gmall.product.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SkuImage;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName: SkuInfoRpcController
 * @Author: WDD
 * @CreateTime: 2022-12-08  21:38
 * @Description: sku
 */
@RestController
@RequestMapping("/api/inner/rpc/product/")
public class SkuRpcController {

    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    SkuImageService skuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    //skuinfo
    @GetMapping("skuinfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable("skuId") Long skuId){
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        return Result.ok(skuInfo);
    }

    //skuimage集合
    @GetMapping("skuimages/{skuId}")
    public Result<List<SkuImage>> getSkuImages(@PathVariable("skuId") Long skuId){
        List<SkuImage> skuImages = skuImageService.lambdaQuery().eq(SkuImage::getSkuId, skuId).list();
        return Result.ok(skuImages);
    }

    //价格
    @GetMapping("skuprice/{skuId}")
    public Result<BigDecimal> getPrice(@PathVariable("skuId") Long skuId){
        SkuInfo one = skuInfoService.lambdaQuery()
                .select(SkuInfo::getPrice)
                .eq(SkuInfo::getId, skuId)
                .one();
        return Result.ok(one.getPrice());
    }

    //spu销售属性名和值集合
    @GetMapping("skusaleattr/{spuId}/{skuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrs(@PathVariable("spuId") Long spuId,@PathVariable("skuId") Long skuId){
        List<SpuSaleAttr> spuSaleAttrs = spuSaleAttrService.getSpuSaleAttrs(spuId,skuId);
        return Result.ok(spuSaleAttrs);
    }

    //valueSkuJson
    @GetMapping("valueSkuJson/{spuId}")
    public Result<String> getValueSkuJson(@PathVariable("spuId") Long spuId){
        String valueSkuJson = spuSaleAttrService.getValueSkuJson(spuId);
        return Result.ok(valueSkuJson);
    }
}
